//
//  LKVisitorView.swift
//  LKWeibo
//
//  Created by like on 16/8/10.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit
import SnapKit

class LKVisitorView: UIView {
    //闭包  01
    var closure:(()->())?
    
     // MARK: button 监听的方法
    @objc private func buttonClick(){
      //闭包03
        closure?()
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     // MARK: - 供外界调用的方法  //为nil代表首页，不为nil是其他视图界面
    func setupVisitorViewInfo(imageName:String?,title:String?){
        
        if let name = imageName,let tit = title{
            
            messageLabel.text = tit
            iconImageView.image = UIImage(named: name)
            
            // 隐藏圆圈
            circleImageView.hidden = true
            
        }else{
            //首页
            setupAnimal()
        }
        
    }
    
     // MARK: - 设置圆圈动画
    func setupAnimal(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置toValue
        anim.toValue = M_PI * 2
        // 设置动画时长
        anim.duration = 10
        // 设置循环次数
        anim.repeatCount = MAXFLOAT
        // 如果用户切换子控制器或者退到后台 默认动画移除
        anim.removedOnCompletion = false
        // 添加动画
        circleImageView.layer.addAnimation(anim, forKey: nil)
        
//        //
//        let ani = CABasicAnimation(keyPath: "transform.rotation")
//        ani.toValue = M_PI_2
//        ani.duration = 10
//        ani.repeatCount = 10
//        anim.removedOnCompletion = false
//        iconImageView.layer.addAnimation(ani, forKey: nil)
//        loginButton.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    
    private func setupUI(){
//        backgroundColor = RandomColor()
        //添加控件，设置约束
        //圆圈
        addSubview(circleImageView)
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //遮挡
        addSubview(maskImageView)
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            
        }
        
        //小房子
        addSubview(iconImageView)
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //文字
        addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.centerX.equalTo(self)
        }
        
        //注册，登陆
        addSubview(loginButton)
        loginButton.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel.snp_leading)
            make.size.equalTo(CGSize(width: 100, height: 35))

        }
        
        addSubview(registButton)
        registButton.snp_makeConstraints { (make) in
            make.size.equalTo(loginButton)
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
        }
    }
    
    // visitordiscover_feed_image_smallicon
    // MARK: - 懒加载控件
    // 圆圈  系统的常规 方法
    private lazy var circleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮挡   便利构造函数
    private lazy var maskImageView:UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    // 房子 便利构造函数
    private lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    //文字lab
    private lazy var messageLabel:UILabel = {
        let lab = UILabel()
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.textColor = appThemeColor
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        lab.font = UIFont.systemFontOfSize(15)
        //最大限宽
        lab.preferredMaxLayoutWidth = 240
        return lab
    }()
    
    

    
    //注册，登陆按钮
    private lazy var loginButton:UIButton = {
        let but = UIButton()
        
        but.addTarget(self, action: #selector(LKVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        but.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        but.setTitle("注册", forState: UIControlState.Normal)
        but.setTitleColor(appThemeColor, forState: UIControlState.Normal)
        but.titleLabel?.font = UIFont.systemFontOfSize(15)
        return but
    }()
    
    private lazy var registButton: UIButton = {
        let but = UIButton()
        but.addTarget(self, action: #selector(LKVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        but.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        but.setTitle("登陆", forState: UIControlState.Normal)
        but.setTitleColor(appThemeColor, forState: UIControlState.Normal)
        but.titleLabel?.font = UIFont.systemFontOfSize(15)
        return but
    }()
    
    
}
